package com.taiyi.renaissancemall.ui.main.first.goodSelect;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.HotBrandBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodSelectActivity extends BaseActivity<GoodSelectView, GoodSelectPresent> implements GoodSelectView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    private int page = 1;
    private List<HotBrandBean.CartListBean> data = new ArrayList<>();
    private GoodSelectAdapter goodSelectAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_good_select;
    }

    @Override
    public GoodSelectPresent initPresenter() {
        return new GoodSelectPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText(getString(R.string.goodSelect));
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);
        setAdapter(data);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        showProgress();
        presenter.getHotBrands(page + "");
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void getHotBrandsSuccess(HotBrandBean hotBrandBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        if (page == 1) {
            data.clear();
            if (hotBrandBean.cartList == null || hotBrandBean.cartList.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(hotBrandBean.cartList);
        if (goodSelectAdapter != null) {
            goodSelectAdapter.notifyDataSetChanged();
        }
    }

    private void setAdapter(List<HotBrandBean.CartListBean> dataBeanList) {
        goodSelectAdapter = new GoodSelectAdapter(R.layout.layout_good_selects, dataBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(GoodSelectActivity.this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(goodSelectAdapter);
    }

    @Override
    public void getHotBrandsFailure(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.backIv})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.backIv) {
            finish();
        }
    }

    @Override
    public void onRefresh() {
        showProgress();
        page = 1;
        presenter.getHotBrands(page + "");
    }

    @Override
    public void onLoadmore() {
        showProgress();
        page++;
        presenter.getHotBrands(page + "");
    }
}
